<?php

namespace app\modules\hr\modules\unit\controllers;
use app\controllers\BaseController;
use app\data\UnitData;

class DefaultController extends BaseController
{


	private $_unitManager;

	public function init(){
		parent::init();
		$this->_unitManager = \YII::$app->mydi->get("app\lib\logic\UnitLogicInterface");
	}

	public function actionGetall(){

		$list = $this->_unitManager->getAll([]);
		return $this->renderJson($list);
	}

	public function actionGetselectall(){
		$unitId = \YII::$app->request->get("unit_id");
		$type = \YII::$app->request->get("type");
		$countryCode = \YII::$app->request->get("country_code");
		$map['type'] = empty($type) ? 4 : $type;
		$map['country_code'] = empty($countryCode) ? 1 : $countryCode;
		if(!empty($unitId)){
			$map['parent_id'] = $unitId;
		}

		$list = $this->_unitManager->getAll($map);
		return $this->renderJson(UnitData::toSelectData($list));
	}

	public function actionGetunit(){
		$unitId = \YII::$app->request->post("unit_id");
		$type =\YII::$app->request->post("type");
		$countryCode = \YII::$app->request->post("country_code");
		$map = ["and"];
		if (empty($type)) {
			$map[] = ["=","type",4];
		}else{
			if (is_numeric($type)){
					$map[] = ["=","type",$type];
			}elseif (is_array($type)){
				$map[] = ["in","type",$type];
			}else{
				return false;
			}
		}
		if(!empty($unitId)){
			$map[] = ['=','parent_id',$unitId];
		}
		if(empty($countryCode)){
			$map[] = ['=','country_code',1];
		}else{
			$map[] = ['=','country_code',$countryCode];
		}
		$list = $this->_unitManager->getAll($map);
		return $this->renderJson(UnitData::toSelectData($list));
	}

}
